package online.flowerinsnow.minecraft.bukkit.manhunt.command;

import online.flowerinsnow.minecraft.bukkit.manhunt.util.MessageUtils;
import org.bukkit.Bukkit;
import org.bukkit.attribute.Attribute;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;
import org.bukkit.entity.Player;
import org.bukkit.event.entity.EntityDamageEvent;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import online.flowerinsnow.minecraft.bukkit.manhunt.util.CommandUtils;

import java.util.List;

public class CommandSuicide implements TabExecutor {
    @Override
    public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) {
        if (args.length == 0) {
            if (!(sender instanceof Player)) {
                MessageUtils.sendMustBePlayer(sender);
                return true;
            }
            if (CommandUtils.checkPermission(sender, "manhunt.suicide")) return true;
            Player player = (Player) sender;
            player.sendMessage(MessageUtils.parseColor("&7[&b!&7]&b您正在尝试自杀，但这可能不起作用...要勇敢地活着..."));
            @SuppressWarnings("ConstantConditions")
            EntityDamageEvent event = new EntityDamageEvent(player, EntityDamageEvent.DamageCause.SUICIDE, player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue() + player.getAbsorptionAmount());
            Bukkit.getPluginManager().callEvent(event);
        } else {
            sender.sendMessage(MessageUtils.parseColor("&7[&b!&7]&b用法: &e/suicide"));
        }
        return true;
    }

    @Nullable
    @Override
    public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String alias, @NotNull String[] args) {
        return null;
    }
}
